<?php defined('SEJUJOPEFDNGJSL') or exit('Access denied');?>
<?php

if(!isset($_GET['templist'])){
    $rr=$config->getpwd();
    $db->query("select * from ld_bank where uname='$_username'",2);
    if(empty($db->rs)){
         $_SESSION["gourl"]="home.php";
         msg_l("您还没有填写银行信息，请先填写!",$backurl."/home.php?lsact=mybank");
    }else{
        $address=$db->rs;
    }
	//获得可以中断收益的提供帮助金额
	$stop_benifit_amount = get_stop_benifit($_username);
?>
<div class="indent-r-kuai-image clear">
	<div class="row">
		<div class="col-md-6">
			<span class="kuai-im im4"><a href="<?php echo SITEINDEX?>?lsact=cunkuan"><img src="theme/images/setbg.png">提供帮助</a></span>
		</div>
		<div class="col-md-6">
			<span class="kuai-im im5" style="margin-right: 0px;"><a href="<?php echo SITEINDEX?>?lsact=qukuan"><img src="theme/images/getbg.png">获取帮助</a></span>
		</div>
    </div>
</div>
<div class="panel panel-inverse">
    <div class="panel-heading">
        <h4 class="panel-title">获取帮助</h4>
    </div>
    <div class="panel-body">
    	<form action="control.php" name="theform" method="post" onsubmit="return chkqk()">
    		<fieldset style="border:none;">
    			<legend>
    				默认银行：<?php echo $address["bankname"]?>&nbsp;&nbsp;&nbsp;&nbsp;
					姓名：<?php echo $address["customer_name"]?>
    			</legend>
				<input type="hidden" name="act" value="quqian">
				<script type="text/javascript">
					qkmin=<?php echo $GLOBALARR['qkmin']; ?>;
					qkbs=<?php echo $GLOBALARR['qkbs']; ?>;
					qkmax=<?php echo $GLOBALARR['qkmax']; ?>;

					ldprizemin=<?php echo $GLOBALARR['ldprizemin']; ?>;
					ldprizebs=<?php echo $GLOBALARR['ldprizebs']; ?>;
				</script>

				<input type="hidden" id="maxbenxi" value="<?php echo ($r['cash_purse']+$stop_benifit_amount); ?>" />
				<input type="hidden" id="maxldprize" value="<?php echo $r['cash_ldprize']; ?>" />
				<!-- 可提本息 -->
				<div class="form-group">
                    <label for="exampleInputEmail1">可提本息：<?php echo ($r['cash_purse']+$stop_benifit_amount); ?></label>
                    <input type="text" name="benxi" id="benxi" class="form-control" />
                    <div class="imporred" style="color: red;">（最低：<?php echo $GLOBALARR['qkmin']; ?>，最高：<?php echo $GLOBALARR['qkmax']; ?>，只能输入<?php echo $GLOBALARR['qkbs'];?>的倍数）</div>
                </div>
				<!--可提本息结束  -->
				<!-- 可提领导奖 -->
				<div class="form-group">
                    <label for="exampleInputEmail1">可提领导奖：<?php echo $r['cash_ldprize']; ?></label>
                    <input type="text" name="ldprize" id="ldprize" class="form-control" />
					<div class="imporred" style="color: red;">（最低：<?php echo $GLOBALARR['ldprizemin']; ?>，只能输入<?php echo $GLOBALARR['ldprizebs'];?>的倍数）</div>
                </div>
				<!-- 密码 -->
				<div class="form-group">
                    <label for="exampleInputPassword1"><?php echo $rr["rgpwd2"]?></label>
                    <input type="password" name="pwd" id="pwd" class="form-control">
                </div>
				<!-- 密码结束 -->
				<button type="submit" class="btn btn-md btn-primary">确定</button>
			</fieldset>
		</form>
    </div>
</div>
<?php
}elseif($_GET['templist']==1){
?>
<div class="right_center">我的获取帮助记录<a href="home.php">返回 >></a></div>
<table width="100%" border="0" cellpadding="0" cellspacing="0" style="margin-top:5px;" class="setlist" align="center">
  <tbody>
    <tr>
        <th>序号</th>
        <th valign="top" width="170">获取帮助编号</th>
        <th valign="top" width="150">获取帮助时间</th>
        <th valign="top">获取帮助金额</th>
        <th valign="top">已配对金额</th>
        <th valign="top">状态</th>
        <th valign="top">配对结束日期</th>
        <th valign="top">操作</th>
    </tr>
<?php
$pagecount=10;
$condition=" where get_help_uid=$_userid";
$psql="select count(0) from ld_get_help $condition";
$db->query($psql,3);
$pcount=$db->fval;
$pagecount=$pagecount;   //每页条数
$astr="&lsact=qukuan&templist=1";    //Url
$pagearr=pagelist($pagecount,$pcount,$astr);
$offset=$pagearr[1];

$sql="select * from ld_get_help $condition order by id desc limit $offset,$pagecount";
$db->query($sql);
$rs=$db->rs;
$ii=0;
foreach ($rs as $k => $v) {
  $ii++;
  $serialid=($pagearr[2]-1)*$pagecount+$ii;
?>
    <tr>
        <td ><?php echo $serialid?></td>
        <td valign="top"><?php echo get_help_no('get',$v['get_help_uname'],$v['get_help_no']);?></td>
        <td valign="top"><?php echo formatdate($v['get_help_time'])?></td>
        <td valign="top"><?php echo formatrmb($v['get_help_amount'])?></td>
        <td valign="top"><?php echo formatrmb($v['matched_amount'])?></td>
        <td valign="top"><?php echo getpeistatus1(array($v["receiver_confirm_amount"],$v["get_help_amount"],$v['status'],$v['receiver_confirm_time'],$v['matched_amount']))?></td>
        <td valign="top"><?php echo formatdate($v['match_end_time'])?></td>
        <td valign="top"><a href="<?php echo $GLOBALARR['curl']?>&templist=2&pdid=<?php echo $v["id"]?>">查看</a>
		</td>
	</tr>
<?php
}
?>
  </tbody>
</table>
	<?php if(($pcount/$pagecount)>1){?>
		<div class="pagediv"><?php echo $pagearr[0]?></div>
	<?php }?>
	<?php if($pcount==0){?>
		<div class="pagediv" style="text-align:center;color:#E47501">--> 暂无数据</div>
<?php
	}
}elseif($_GET['templist']==2){
	if($_POST["submit"]=="确认收款"){
		if($_POST["imgid"]<1) msg_l("操作失败!",$GLOBALARR['curl']);
		//根据id选取汇款记录
		$db->query("select * from ld_remit_list where id=".intval($_POST["imgid"]),2);
		$rs=$db->rs;
		if(empty($db->rs)) msg_l("操作失败!",$GLOBALARR['curl']);
		if(!empty($db->rs["is_finnished"]))  msg_l("操作失败!",$GLOBALARR['curl']);
		try{
			$db->begintransaction();
			//更新汇款表，状态改为已结束，记录确认收款时间
			$db->sitedbexec("update ld_remit_list set is_finnished=1,remittee_confirm_time=".time()." where id=".intval($_POST["imgid"]));
			//更新提供帮助表，增加提供帮助人确认打款金额
			$db->sitedbexec("update ld_give_help set helper_confirm_amount=helper_confirm_amount+".$rs["amount"]." where id=".$rs["give_help_id"]);
			//根据id选取提供帮助记录
			$db->query("select amount,helper_confirm_amount,uname,uid,matched_time from ld_give_help where id=".$rs["give_help_id"],2);
			$gps=$db->rs;
			//如果提供帮助人已确认金额等于提供帮助总额
			if($gps["amount"]==$gps["helper_confirm_amount"]){
				//记录提供帮助证明上传时间是当前时间
				$db->sitedbexec("update ld_give_help set giveh_eviup_time=".time()." where id=".$rs["give_help_id"]);
				//增加累计提供帮助金额
				$db->sitedbexec("update ld_user_account set sum_deposit=sum_deposit+".$gps["amount"]." where id=".$gps["uid"]);

				//取得提供帮助人信息
				$row=$member->getuv($gps["uname"],'introducer_id,had_ghelp_mark,id,uname');
				if($row["had_ghelp_mark"]==0){
					//记录已经完成了一次提供帮助
					$db->sitedbexec("update ld_user_account set had_ghelp_mark=1 where had_ghelp_mark=0 and id=".$row["id"]);
				}
			}

			//更新获得帮助表，增加获得帮助人确认金额
			$db->sitedbexec("update ld_get_help set receiver_confirm_amount=receiver_confirm_amount+".$rs["amount"]." where id=".$rs["get_help_id"]);
			$db->query("select * from ld_get_help where id=".$rs["get_help_id"],2);
			$gqs=$db->rs;
			//如果获得帮助总额等于获得帮助人已确认金额
			if($gqs["get_help_amount"]==$gqs["receiver_confirm_amount"]){
				//更新获得帮助表，记录获得帮助确认时间
				$db->sitedbexec("update ld_get_help set receiver_confirm_time=".time()." where id=".$rs["get_help_id"]);
			}

			//发放领导奖
			fun_jdjshouyi($rs["give_help_id"]);
			$db->committransaction();
			record_log("确认收款成功,id为".$_POST["imgid"],0);
			msg_l("确认收款成功 ",'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
		}catch(PDOException $e){
			$db->rollbacktransaction();
			a_bck("error");
		}
	}
	if(!isset($_GET["pdid"])) locationurl("home.php?lsact=qukuan&templist=1");
	if($_GET["pdid"]<1) locationurl("home.php?lsact=qukuan&templist=1");
	//根据传过来的参数选取获取帮助记录
    $db->query("select * from ld_get_help where get_help_uid=$_userid and id=".$_GET["pdid"],2);
    if(empty($db->rs)) locationurl("home.php?lsact=qukuan&templist=1");
    $pds=$db->rs;
    $zuofei=$pds["status"];
    $arrinfo=array();
	//根据获取表中的id 从匹配表中读取记录
    $db->query("select * from ld_remit_list where get_help_id=".$pds["id"]." order by id desc");
    $rs=$db->rs;
	foreach ($rs as $k => $v) {
		$arrinfo[$k]["jine"]=$v["amount"];
		$arrinfo[$k]["jiaodate"]=date("Y-m-d H:i:s",$v["deal_time"]);
		$arrinfo[$k]["pdid"]=$v["id"];
		$arrinfo[$k]["img"]=$v["certificates"];
		$arrinfo[$k]["evidence_upload_time"]=$v["evidence_upload_time"];
		$arrinfo[$k]["is_finnished"]=$v["is_finnished"];
		$arrinfo[$k]["deal_time"]=$v["deal_time"];
		$arrinfo[$k]["remittee_confirm_time"]=$v["remittee_confirm_time"];
		$arrinfo[$k]["is_overdate"]=$v["is_overdate"];

		//取得获得帮助信息
		$db->query("select * from ld_get_help where id=".$v["get_help_id"],2);
		//取得获得帮助人信息
		$us=$member->getuserinfo($db->rs["get_help_uid"]);
		$arrinfo[$k]["get_help_name"]=$us['uname'];
		$arrinfo[$k]["mobile"]=$us["phone"];
		$arrinfo[$k]["weixin"]=$us["rgweixin"];
		$arrinfo[$k]["get_help_rname"]=$us["real_name"];
		//取得银行资料
		$db->query("select * from ld_bank where uname='".$us["uname"]."' order by is_default desc");
		$bankarr = $db->rs;
		$arrinfo[$k]["bank"] = '';
		foreach($bankarr as $val){
			$arrinfo[$k]["bank"] .= "<br /> --- ".$val['bankname']." 卡号： ".$val['bankno'];
		}
		if(!empty($us["introducer_id"])){
			$ldrow=$member->getuserinfo($us["introducer_id"]);
			$arrinfo[$k]["ldid"]=$ldrow["uname"];
			$arrinfo[$k]["ldmobile"]=$ldrow["phone"];
			$arrinfo[$k]["ldrgweixin"]=$ldrow["rgweixin"];
			$arrinfo[$k]["ldrname"]=$ldrow["real_name"];
		}else{
			$arrinfo[$k]["ldid"]="";
			$arrinfo[$k]["ldmobile"]="";
			$arrinfo[$k]["ldrgweixin"]="";
			$arrinfo[$k]["ldrname"]="";
		}

		//取得提供帮助信息
		$db->query("select * from ld_give_help where id=".$v["give_help_id"],2);
		$arrinfo[$k]["status"]=getstatutype($db->rs['status'],8);
		//取得提供帮助人信息
		$give_help_user = $member->getuserinfo($db->rs['uid']);
		$arrinfo[$k]["give_help_uname"]=$give_help_user["uname"];
		$arrinfo[$k]["give_help_rname"]=$give_help_user["real_name"];
		$arrinfo[$k]["give_help_wx"]=$give_help_user["rgweixin"];
		$arrinfo[$k]["give_help_phone"]=$give_help_user["phone"];
		//取得提供帮助的用户的介绍人（上线）
		if(!empty($give_help_user["introducer_id"])){
			$ldrow=$member->getuserinfo($give_help_user["introducer_id"]);
			$arrinfo[$k]["ldid1"]=$ldrow["uname"];
			$arrinfo[$k]["ldmobile1"]=$ldrow["phone"];
			$arrinfo[$k]["ldrgweixin1"]=$ldrow["rgweixin"];
			$arrinfo[$k]["ldrname1"]=$ldrow["real_name"];
		}else{
			$arrinfo[$k]["ldid1"]="";
			$arrinfo[$k]["ldmobile1"]="";
			$arrinfo[$k]["ldrgweixin1"]="";
			$arrinfo[$k]["ldrname1"]="";
		}
	}
?>
<!-- 帮助记录帮助记录 -->
	<style type="text/css">
		.baitab11 td{margin-top: 10px;}
	</style>
	<div class="panel panel-inverse">
        <div class="panel-heading">
            <div class="row">
                <div class="col-md-6">
                    <h4 class="panel-title">获取帮助配对信息</h4>
                </div>
                <div class="col-md-6 text-right">
                    <a href="home.php">返回 >></a>
                </div>
            </div>
        </div>
        <div class="panel-body">
        	<table border="0" style="margin:0 auto;margin-top:10px;" cellpadding="0" cellspacing="0" class="baitab baitab11" align="center">
				<tbody>
				<?php
				if(count($arrinfo)==0){
					if($zuofei==3){
				?>
					<tr>
						<td width="25%" valign="top" align="center" colspan="4" style="background:#552A15;font-size:16px;color:#F0E531">已取消</td>
					</tr>
				<?php
					}else{
				?>
					<tr>
						<td width="25%" valign="top" align="center" colspan="4" style="background:#E5E5E5;font-size:16px;color:blue">暂无配对信息</td>
					</tr>
				<?php
					}
				}
				foreach ($arrinfo as $k => $v) {
				?>
					<tr>
						<td width="25%" valign="top" align="left" colspan="4"><p style="border-radius: 3px;background:#99dede;text-indent: 15px;line-height: 40px;margin-bottom: 15px;font-size: 16px;"><b>配对目标<?php echo count($arrinfo)-$k;?></b>
							</p>
						</td>
					</tr>
						<style type="text/css">
							.getable td{border:none!important;padding:0 10px;}
						</style>
					<tr>
						<td colspan="2">
							<div class="row">
								<div class="col-md-6">
									<div class="panel-heading">
			                            <h4 class="panel-title">收款人</h4>
			                        </div>
			                        <div class="panel-body" style="border:1px solid #ccc;">
										<table class="table table-striped" cellpadding="0" cellspacing="0">
											<tr>
												<td>收款人信息</td>
												<td>收款人领导信息</td>
											</tr>
											<tr>
												<td>用户名：<?php echo $v["get_help_name"]?></td>
												<td>用户名：<?php echo $v["ldid"]?></td>
											</tr>
											<tr>
												<td>姓名：<?php echo $v['get_help_rname']?></td>
												<td>姓名：<?php echo $v['ldrname']?></td>
											</tr>
											<tr>
												<td>微信：<?php echo $v['weixin']?></td>
												<td>微信：<?php echo $v['ldrgweixin']?></td>
											</tr>
											<tr>
												<td>联系电话：<?php echo $v['mobile']?></td>
												<td>联系电话：<?php echo $v['ldmobile']?></td>
											</tr>
											<tr><td colspan="2">RMB：<?php echo $v['jine']?></td></tr>
											<tr><td colspan="2">银行卡：<?php echo $v['bank']?></td></tr>
										</table>
									</div>
								</div>
								<div class="col-md-6">
									<div class="panel-heading">
			                            <h4 class="panel-title">付款人</h4>
			                        </div>
			                        <div class="panel-body" style="border:1px solid #ccc;">
									<table class="table table-striped" cellpadding="0" cellspacing="0">
										<tr>
											<td>付款人信息</td>
											<td>付款人领导信息</td>
										</tr>
										<tr>
											<td>用户名：<?php echo $v['give_help_uname']?></td>
											<td>用户名：<?php echo $v['ldid1']?></td>
										</tr>
										<tr>
											<td>姓名：<?php echo $v['give_help_rname']?></td>
											<td>姓名：<?php echo $v['ldrname1'] ?></td>
										</tr>
										<tr>
											<td>微信：<?php echo $v['give_help_wx']?></td>
											<td>微信：<?php echo $v['ldrgweixin1']?></td>
										</tr>
										<tr>
											<td>联系电话：<?php echo $v['give_help_phone']?></td>
											<td>联系电话：<?php echo $v['ldmobile1']?></td>
										</tr>
									</table>
								</div>
							</div>
						</td>
					</tr>
                    <tr>
                        <td colspan="2" height="15px"></td>
                    </tr>
					<tr>
						<td colspan="2">
							<div class="row">
								<div class="col-md-6">
									<div class="panel-heading">
			                            <h4 class="panel-title">交易日期</h4>
			                        </div>
			                        <div class="panel-body" style="border:1px solid #ccc;">
										<table class="table table-striped">
											<tr>
												<td>交易日期</td>
												<td>倒计时</td>
											</tr>
											<tr>
												<td><?php echo $v['jiaodate']?></td>
												<td><?php
													if($v["is_finnished"]==0 && $v['is_overdate']==0){
														if(empty($v["evidence_upload_time"])){
															$time=$v["deal_time"];
															$counttime=$GLOBALARR["jjqkok"];
														}else{
															$time=$v["evidence_upload_time"];
															$counttime=$GLOBALARR["jjtx"];
														}
													?>
														<span class="J_left_time<?php echo $k?> dsj" rel="<?php echo strtotime("+".$counttime." minute",$time)-time()?>" id="dsj" >
															<span class="s day">--</span>
															<span class="l">天</span>
															<span class="s hour">--</span>
															<span class="l">时</span>
															<span class="s min">--</span>
															<span class="l">分</span>
															<span class="s sec">--</span>
															<span class="l">秒</span>
														</span>
													</td>
													<script type="text/javascript">
														var leftTimeActInv<?php echo $k?> = null;
														jQuery(function(){
															leftTimeAct<?php echo $k?>();
														});
														function leftTimeAct<?php echo $k?>(){
															clearTimeout(leftTimeActInv<?php echo $k?>);
															$(".J_left_time<?php echo $k?>").each(function(){
																var leftTime = parseInt($(this).attr("rel"));
																if(leftTime > 0)
																{
																	var day  =  parseInt(leftTime / 24 /3600);
																	 var hour = parseInt((leftTime % (24 *3600)) / 3600);
																	//var hour = parseInt(leftTime / 3600);
																	var min = parseInt((leftTime % 3600) / 60);
																	var sec = parseInt((leftTime % 3600) % 60);
																	$(this).find(".day").html((day));
																	//$(this).find(".day").hide();
																	$(this).find(".hour").html((hour));
																	$(this).find(".min").html((min));
																	$(this).find(".sec").html((sec));
																	leftTime--;
																	$(this).attr("rel",leftTime);
																}
																else{
																	$(this).css("background","none");
																	$(this).html('时间已结束');
																}
															});

															leftTimeActInv<?php echo $k?> = setTimeout(function(){
																leftTimeAct<?php echo $k?>();
															},1000);
														}
													</script>
												<?php
												}
												?>
											</tr>
										</table>
									</div>
								</div>
								<div class="col-md-6">
									<div class="panel-heading">
			                            <h4 class="panel-title">交易状态</h4>
			                        </div>
			                        <div class="panel-body" style="border:1px solid #ccc;">
									<table class="table table-striped" cellpadding="0" cellspacing="0">
										<tr>
											<td>状态</td>
											<td>上传凭证</td>
										</tr>
										<tr>
                                            <td>
                                                <?php  echo getcunstatus1(array("is_finnished"=>$v["is_finnished"],"evidence_upload_time"=>$v["evidence_upload_time"],"is_overdate"=>$v["is_overdate"]));?>
                                            </td>
                                            <td>
                                                    <?php
                                                    if(!empty($v["img"])){
                                                    ?>
                                                        &nbsp;已上传&nbsp;&nbsp;<?php if(empty($v["evidence_upload_time"])){echo "提供帮助人未确认汇款";}?>&nbsp;&nbsp;<a href="javascript:artload1('<img src=<?php echo YUMING.'upfile/'.$v["img"]?>>');">查看上传</a>
                                                    <?php
                                                    }else{
                                                    ?>
                                                        未上传图片
                                                    <?php
                                                    }
                                                    if($v["is_finnished"]==0&&$v["is_overdate"]==0&&$v["evidence_upload_time"]>0&&!empty($v["img"])){
                                                    ?>
                                                        <form action="home.php?lsact=qukuan&templist=2&pdid=<?php echo $_GET["pdid"]?>" method="post" >
                                                            <input type="hidden" name="imgid" size="30" value="<?php echo $v["pdid"]?>">
                                                            &nbsp;<input type="submit" name="submit" value="确认收款" onclick="return confirm('您确认了吗?')" class="btn btn-success" style="margin-top: 5px;">
                                                        </form>
                                                    <?php
                                                    }
                                                    ?>
                                                    </td>
                                                </tr>
									</table>
								</div>
							</div>
						</td>
					</tr>
                    <tr><td colspan="2" height="15px"></td></tr>
				<?php
				}
				?>
		        </tbody>
		    </table>
       	</div>
    </div>
<?php }?>
